/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import javax.swing.JPanel;
import org.balisunrise.application.Application;
import org.balisunrise.builder.FactoryException;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.Query;
import org.balisunrise.common.Criterionable;
import org.balisunrise.common.EntityLoader;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractEntityFilter;
import org.balisunrise.swing.builder.FieldLayoutBuilder;
import org.balisunrise.swing.components.util.EntityFilterFocusTraversal;
import org.balisunrise.swing.components.util.FocusManager;

/**
 *
 * @author Glauco
 */
public class EntityFilter extends AbstractEntityFilter
        implements org.balisunrise.common.EntityFilter<JPanel>{

    private JPanel visualRepresentation;

    private EntityFilterFocusTraversal focusTraversal;

    private FieldLayoutBuilder fieldLayoutBuilder;

    public EntityFilter(FieldLayoutBuilder fieldLayoutBuilder) {
        this.fieldLayoutBuilder = fieldLayoutBuilder;
    }

    @Override
    public void setEntityLoader(EntityLoader entityLoader) {
        super.setEntityLoader(entityLoader);
        createFocusTraversal();
    }

    public void setVisualRepresentation(JPanel visualRepresentation) {
        JPanel old = this.visualRepresentation;
        this.visualRepresentation = visualRepresentation;
        changeSupport.firePropertyChange("visualRepresentation",
                old, visualRepresentation);
        createFocusTraversal();
    }

    public Query getQuery() {
        try{
            Query query = Application.get().getFactory().createBaliQuery(entityClass);
            query.setOwner(this);
            for (org.balisunrise.common.FieldLayout fl : layouts) {
                if(fl instanceof Criterionable){
                    Criterionable c = (Criterionable) fl;
                    query.addCriterion(c.getCriterion());
                }
            }
            return query;
        } catch (FactoryException fex){
            System.out.println(fex.getMessage());
        }
        return null;
    }

    /**
     * Retorna a representação visual.
     */
    public JPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    private void createFocusTraversal(){
        if(visualRepresentation != null && entityLoader != null){
            if(focusTraversal != null)
                FocusManager.getInstance().removeFocusTraversal(focusTraversal);
            focusTraversal = new EntityFilterFocusTraversal(this);
            FocusManager.getInstance().addFocusTraversal(focusTraversal);
        }
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará ENTITY_FILTER.
     * @return VisualEntityType.ENTITY_FILTER
     */
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.ENTITY_FILTER;
    }

    /**
     * Verifica se algum Criterion foi modificado depois da chamada getQuery().
     */
    public boolean isModified() {
        for (org.balisunrise.common.FieldLayout fl : layouts) {
                if(fl instanceof Criterionable){
                    Criterionable c = (Criterionable) fl;
                    if(c.isModified())
                        return true;
                }
            }
        return false;
    }

    public void buildLayout() {
        try{
            fieldLayoutBuilder.create(visualRepresentation, layouts);
        }catch(BuilderException bex){
            System.out.println("Falha ao construir Layout: " + bex.getMessage());
        }
    }
}
